﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nop.Core.Data;
using Nop.Core.Domain.Catalog;
using Nop.Core.Events;
using Nop.Core.Caching;
using Nop.Core;

namespace Nop.Services.Catalog
{
    public class ProductClickLogService : IProductClickLogService
    {
         #region Constants
        private const string MERAPP_BY_ID_KEY = "Nop.productclicklog.id-{0}";
        private const string MERAPP_PATTERN_KEY = "Nop.productclicklog.";
        #endregion

        #region Fields
        private readonly IRepository<ProductClickLog> _productClickLogRepository;
        private readonly IEventPublisher _eventPublisher;
        private readonly ICacheManager _cacheManager;
        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="manufacturerRepository">Category repository</param>
        /// <param name="productManufacturerRepository">ProductCategory repository</param>
        /// <param name="productRepository">Product repository</param>
        /// <param name="eventPublisher">Event published</param>
        public ProductClickLogService(ICacheManager cacheManager,
            IRepository<ProductClickLog> productclicklogRepository,
            IEventPublisher eventPublisher)
        {
            _cacheManager = cacheManager;
            _productClickLogRepository = productclicklogRepository;
            _eventPublisher = eventPublisher;
        }
        #endregion

        #region Methods

        /// <summary>
        /// Deletes a news
        /// </summary>
        /// <param name="newsItem">News item</param>
        public virtual void DeleteProductClickLog(ProductClickLog Item)
        {
            if (Item == null)
                throw new ArgumentNullException("ProductClickLog");

            _productClickLogRepository.Delete(Item);

            _cacheManager.RemoveByPattern(MERAPP_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityDeleted(Item);
        }

        /// <summary>
        /// Gets a news
        /// </summary>
        /// <param name="newsId">The news identifier</param>
        /// <returns>News</returns>
        public virtual ProductClickLog GetProductClickLogById(int Id)
        {
            if (Id == 0)
                return null;

            string key = string.Format(MERAPP_BY_ID_KEY, Id);
            return _cacheManager.Get(key, () =>
            {
                var n = _productClickLogRepository.GetById(Id);
                return n;
            });
        }

        /// <summary>
        /// Gets all news
        /// </summary>
        /// <param name="languageId">Language identifier; 0 if you want to get all records</param>
        /// <param name="dateFrom">Filter by created date; null if you want to get all records</param>
        /// <param name="dateTo">Filter by created date; null if you want to get all records</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>News items</returns>
        public virtual IPagedList<ProductClickLog> GetAllProClickLog(int languageId,
            DateTime? dateFrom, DateTime? dateTo, int pageIndex, int pageSize, bool showHidden = false)
        {
            var query = _productClickLogRepository.Table;

            query = query.OrderByDescending(b => b.Date);

            var news = new PagedList<ProductClickLog>(query, pageIndex, pageSize);
            return news;
        }

        /// <summary>
        /// Gets all news
        /// </summary>
        /// <param name="languageId">Language identifier; 0 if you want to get all records</param>
        /// <param name="dateFrom">Filter by created date; null if you want to get all records</param>
        /// <param name="dateTo">Filter by created date; null if you want to get all records</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>News items</returns>
        public virtual IPagedList<ProductClickLog> GetAllMerApp(int pageIndex, int pageSize, bool showHidden = false)
        {
            var query = _productClickLogRepository.Table;

            query = query.OrderByDescending(b => b.Date);

            var news = new PagedList<ProductClickLog>(query, pageIndex, pageSize);
            return news;
        }

        /// <summary>
        /// Inserts a news item
        /// </summary>
        /// <param name="news">News item</param>
        public virtual void InsertMerApp(ProductClickLog item)
        {
            if (item == null)
                throw new ArgumentNullException("ProductClickLog");

            _productClickLogRepository.Insert(item);

            _cacheManager.RemoveByPattern(MERAPP_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(item);
        }

        /// <summary>
        /// Updates the news item
        /// </summary>
        /// <param name="news">News item</param>
        public virtual void UpdateProClickLog(ProductClickLog Item)
        {
            if (Item == null)
                throw new ArgumentNullException("ProductClickLog");

            _productClickLogRepository.Update(Item);

            _cacheManager.RemoveByPattern(MERAPP_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityUpdated(Item);
        }
        #endregion
    }
}
